﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using APH.Parl2.Data.DataAccess.Repository;
using APH.Parl2.Data.Model.Publishing.WriteModel;
using System.Configuration;

namespace APH.Parl2.Data.DataAccess.Publishing
{
    public static class WriteHelper
    {
        //private static string _connectionString = "metadata=res://*/WriteModel.WriteModel.csdl|res://*/WriteModel.WriteModel.ssdl|res://*/WriteModel.WriteModel.msl;provider=System.Data.SqlClient;provider connection string='Data Source=localhost;Initial Catalog=Parl2_Write;Integrated Security=True'";
        
        /// <summary>
        /// Gets or sets the connection string.
        /// </summary>
        /// <value>The connection string.</value>
        public static string ConnectionString
        {
            get
            {
                //return _connectionString;
                return ConfigurationManager.ConnectionStrings["WriteModelContainer"].ConnectionString;
            }
            //set
            //{ 
            //    _connectionString = value;
            //}
        }

        /// <summary>
        /// Creates the document.
        /// </summary>
        /// <param name="author">The author.</param>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static Guid CreateDocument(string author, string data)
        {
            using (var context = new WriteModelContainer(ConnectionString))
            {
                var contextAdapter = new ObjectContextAdapter(context);
                var unitOfWork = new UnitOfWork(contextAdapter);
                var rep = new Repository<Document>(contextAdapter);

                var doc = new Document();
                doc.Data = data;
                doc.Author = author;

                rep.Add(doc);
                unitOfWork.Commit();

                return doc.Id;
            }
        }

        /// <summary>
        /// Deletes the document.
        /// </summary>
        /// <param name="id">The id.</param>
        public static void DeleteDocument(Guid id)
        {
            using (var context = new WriteModelContainer(ConnectionString))
            {
                var contextAdapter = new ObjectContextAdapter(context);
                var unitOfWork = new UnitOfWork(contextAdapter);
                var rep = new Repository<Document>(contextAdapter);

                var doc = GetDocumentById(id);
                rep.Attach(doc);
                rep.Delete(doc);
                unitOfWork.Commit();
            }
        }

        /// <summary>
        /// Updates the document content.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="data">The data.</param>
        public static void UpdateDocumentContent(Guid id, string data)
        {
            using (var context = new WriteModelContainer(ConnectionString))
            {
                var contextAdapter = new ObjectContextAdapter(context);
                var unitOfWork = new UnitOfWork(contextAdapter);
                var rep = new Repository<Document>(contextAdapter);
               
                var doc = GetDocumentById(id);
                if (doc != null)
                {
                    rep.Attach(doc);
                
                    doc.Data = data;
                    doc.UpdatedDate = DateTime.Now;

                    unitOfWork.Commit();
                }
            }
        }

        /// <summary>
        /// Gets the document by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static Document GetDocumentById(Guid id)
        {
            using (var context = new WriteModelContainer(ConnectionString))
            {
                var contextAdapter = new ObjectContextAdapter(context);
                var rep = new Repository<Document>(contextAdapter);

                var res = rep.First(d => d.Id == id);
                return res;
            }
        }
    }
}
